package com.rqf.ls.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oreilly.servlet.ParameterNotFoundException;
import com.rqf.common.base.PathManager;
import com.rqf.common.base.ProjectInfor;
import com.rqf.ls.service.IFileUploadService;
import com.rqf.ls.service.IQuestionAnswerService;
import com.rqf.ls.service.IRegisterOfLawyerService;
import com.rqf.ls.service.IRegisterService;
import com.rqf.ls.service.IRemindService;
import com.server.password.MyBase64;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;

/**
 * 咨询相关
 * @author ServerZhang
 * @date 2016年3月28日
 */
@Controller
@RequestMapping("questionAnswer")
public class QuestionAnswerController {
	
	@Autowired
	private IQuestionAnswerService questionAnswerService;
	@Autowired
	private IRemindService remindservice;
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private IRegisterOfLawyerService registerOfLawyerService;
	@Autowired
	private IFileUploadService fileUploadService;
	

	@RequestMapping("commitQuestion")
	public void commitQuestion(HttpServletRequest request, 
			HttpServletResponse response){
		String phoneNum = request.getParameter("phoneNum");
		String question_type_id = request.getParameter("question_type");
		String question_content = request.getParameter("question_content");
		String askwhoid = request.getParameter("askwhoid");
		
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("content", question_content);
		inputMap.put("createuser", phoneNum);
		if (StringUtils.isEmpty(question_type_id)) {
			question_type_id = "0";
		}
		inputMap.put("questiontypeid", question_type_id);
		if (!StringUtils.isEmpty(askwhoid)) {
			Boolean sendMsg = remindservice.sendMsg(askwhoid, "暂无");
			//TODO 发送短信失败时处理
			inputMap.put("askwhoid", askwhoid);
		}
		
		Boolean isSuccess = questionAnswerService.add(inputMap);
		
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	@RequestMapping("commitVoiceQuestion")
	public void commitVoiceQuestion(HttpServletRequest request, 
			HttpServletResponse response){
		Boolean isSuccess = false;
		String phoneNum = request.getParameter("phoneNum");
		if (StringUtils.isEmpty(phoneNum)) {
			OutputUtil.returnJsonp(request, response, isSuccess.toString());
			return;
		}
		String question_type_id = request.getParameter("question_type");
		//String question_content = request.getParameter("question_content");
		String askwhoid = request.getParameter("askwhoid");
		
		String type = request.getParameter("type");
		String voice_length = request.getParameter("voice_length");
		//处理 录音
		String base64Voice = request.getParameter("voice_content");
		String voiceName = request.getParameter("voice_name");
		Boolean uploadVoiceInfor = false;
		String attachmentId = "";
		if (!StringUtils.isEmpty(voiceName) && !StringUtils.isEmpty(base64Voice)) {
			// 录音数据
			String[] split = base64Voice.split("base64,");
			String tempStr = split[1];
			String realStr = tempStr.substring(0, tempStr.length() - 2);
			byte[] decode = MyBase64.decode(realStr);

			// 上传录音及保存数据库
			Map<String, Object> others = new HashMap<String, Object>();
			others.put("saveType", PathManager.FILE_SAVE_TYPE_VOICE);// 必须的
			try {
				attachmentId = fileUploadService.upload1(
						ProjectInfor.PROJECT_NAME, phoneNum,
						PathManager.FILE_SAVE_TYPE_VOICE, voiceName, decode,
						others);
			} catch (ParameterNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String voicePath = "";
		if ( !StringUtils.isEmpty(attachmentId) ) {
			uploadVoiceInfor = true;
			voicePath = fileUploadService.getFilePathById(attachmentId);
		}
		
		if ( !uploadVoiceInfor ) {
			OutputUtil.returnJsonp(request, response, uploadVoiceInfor.toString());
			return;
		}
		String sourcePath = voicePath;
		String targetPath = voicePath.replaceFirst(".wav", ".mp3");
		fileUploadService.amrChangeToMp3(sourcePath, targetPath);
		voicePath = targetPath;
		
		//添加问题
		Map<String, Object> inputMap = new HashMap<String, Object>();
		//inputMap.put("content", question_content);
		inputMap.put("type", type);
		inputMap.put("voice", voicePath);
		inputMap.put("createuser", phoneNum);
		inputMap.put("voicelength", voice_length);
		if (StringUtils.isEmpty(question_type_id)) {
			question_type_id = "0";
		}
		inputMap.put("questiontypeid", question_type_id);
		if (!StringUtils.isEmpty(askwhoid)) {
			Boolean sendMsg = remindservice.sendMsg(askwhoid, "暂无");
			//TODO 发送短信失败时处理
			inputMap.put("askwhoid", askwhoid);
		}
		
		isSuccess = questionAnswerService.add(inputMap);
		
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	@RequestMapping("getQuestionList")
	public void getQuestionList(HttpServletRequest request, 
			HttpServletResponse response){
		String phoneNum = request.getParameter("phoneNum");
		/*if (StringUtils.isEmpty(phoneNum)) {
			String lawyerPhone = request.getParameter("lawyerPhone");
			questionList = questionAnswerService.questionList();
			if (!StringUtils.isEmpty(lawyerPhone)) {
				Boolean setRead = remindservice.setQuestionRead(lawyerPhone);
				//TODO 如果设置为已读时出错提示
			}
		}else {
			questionList = questionAnswerService.questionList(phoneNum);
		}*/
		DataVector<Map<String, Object>> listData = questionAnswerService.questionList(phoneNum, null, new RequestInput(request));
		List<Map<String, Object>> questionList = listData.getList();
		
		Iterator<Map<String, Object>> iterator = questionList.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("id");
			if (idObj != null) {
				Integer answerCount = questionAnswerService.answerCount(Integer.parseInt(idObj.toString()));
				next.put("answerCount", answerCount);
			}
			Object userObj = next.get("createUser");
			if (userObj != null) {
				String photoPath = registerService.getPhotoPath(userObj.toString());
				next.put("imgPath", photoPath);
			}
		}
		String json = JSONSerielizer.toJson(questionList, null, null);
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnString(response, json);
		//OutputUtil.returnJsonp(request, response, JSONSerielizer.toJson(questionList, null, null));
	}
	
	 /**
	 * 针对律师的问题列表
	 * @param request
	 * @param response
	 * @param lawyerPhone
	 * @param answered
	 * @author ServerZhang
	 * @date 2016年4月1日
	 */
	@RequestMapping("qList/{lawyerPhone}/{answered}")
	public void qList(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String lawyerPhone,
			@PathVariable Integer answered){
		
		DataVector<Map<String, Object>> listData = questionAnswerService.questionList(null, lawyerPhone, new RequestInput(request));
		List<Map<String, Object>> questionList = listData.getList();
		
		List<Map<String, Object>> answeredList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> noAnswereList = new ArrayList<Map<String,Object>>();
		Iterator<Map<String, Object>> iterator = questionList.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("id");
			if (idObj != null) {
				Integer answerCount = questionAnswerService.answerCount(Integer.parseInt(idObj.toString()));
				if (answerCount > 0) {
					answeredList.add(next);
				}else {
					noAnswereList.add(next);
				}
				next.put("answerCount", answerCount);
			}
			Object userObj = next.get("createUser");
			if (userObj != null) {
				String photoPath = registerService.getPhotoPath(userObj.toString());
				next.put("imgPath", photoPath);
			}
		}
		String json = null;
		if (answered.equals(1)) {
			json = JSONSerielizer.toJson(answeredList, null, null);
		}else if (answered.equals(0)) {
			json = JSONSerielizer.toJson(noAnswereList, null, null);
		} else {
			json = JSONSerielizer.toJson(questionList, null, null);
		}
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnString(response, json);
		
	}
	
	@RequestMapping("isAnswer")
	public void isAnswer(HttpServletRequest request, 
			HttpServletResponse response){
		String questionid = request.getParameter("questionid");
		String lawyerPhoneNum = request.getParameter("phoneNum");
		Boolean isSuccess = false;
		List<Map<String, Object>> answerList = questionAnswerService.getAnswer(questionid, lawyerPhoneNum);
		Map<String, Object> answer = null;
		if ( answerList.size()>0 ) {
			answer = answerList.get(0);
		}
		if ( answer==null ) {//判断 问题 该律师是否已经回答过
			isSuccess = true;
		}
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	@RequestMapping("answer")
	public void answer(HttpServletRequest request, 
			HttpServletResponse response){
		String anwserwho = request.getParameter("anwserwho");
		String content = request.getParameter("content");
		String questionid = request.getParameter("questionid");
		String whoque = request.getParameter("whoque");
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("content", content);
		inputMap.put("whoque", whoque);
		
		Boolean isSuccess = false;
		List<Map<String, Object>> answerList = questionAnswerService.getAnswer(questionid, anwserwho);
		Map<String, Object> answer = null;
		if ( answerList.size()>0 ) {
			answer = answerList.get(0);
		}
		if ( answer!=null ) {//判断 问题 该律师是否已经回答过
			OutputUtil.returnJsonp(request, response, isSuccess.toString());
			return;
		}
		
		isSuccess = questionAnswerService.answerAdd(Integer.parseInt(questionid), anwserwho, inputMap);
		
		if ( isSuccess ) {
			Map<String, Object> inputMap1 = new HashMap<String, Object>();
			inputMap1.put("ISANSWER", 1);
			isSuccess = questionAnswerService.update(Integer.parseInt(questionid), inputMap);
		}
		
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	@RequestMapping("answerVoice")
	public void answerVoice(HttpServletRequest request, 
			HttpServletResponse response){
		String anwserwho = request.getParameter("anwserwho");//phoneNum
		String content = request.getParameter("content");
		String questionid = request.getParameter("questionid");
		String whoque = request.getParameter("whoque");
		
		String type = request.getParameter("type");
		String voice_length = request.getParameter("voice_length");
		//处理 录音
		String base64Voice = request.getParameter("voice_content");
		String voiceName = request.getParameter("voice_name");
		Boolean uploadVoiceInfor = false;
		String attachmentId = "";
		if (!StringUtils.isEmpty(voiceName) && !StringUtils.isEmpty(base64Voice)) {
			// 录音数据
			String[] split = base64Voice.split("base64,");
			String tempStr = split[1];
			String realStr = tempStr.substring(0, tempStr.length() - 2);
			byte[] decode = MyBase64.decode(realStr);

			// 上传录音及保存数据库
			Map<String, Object> others = new HashMap<String, Object>();
			others.put("saveType", PathManager.FILE_SAVE_TYPE_VOICE_LAWYER);// 必须的
			try {
				attachmentId = fileUploadService.upload1(
						ProjectInfor.PROJECT_NAME, anwserwho,
						PathManager.FILE_SAVE_TYPE_VOICE_LAWYER, voiceName, decode,
						others);
			} catch (ParameterNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String voicePath = "";
		if ( !StringUtils.isEmpty(attachmentId) ) {
			uploadVoiceInfor = true;
			voicePath = fileUploadService.getFilePathById(attachmentId);
		}
		
		if ( !uploadVoiceInfor ) {
			OutputUtil.returnJsonp(request, response, uploadVoiceInfor.toString());
			return;
		}
		
		String sourcePath = voicePath;
		String targetPath = voicePath.replaceFirst(".wav", ".mp3");
		fileUploadService.amrChangeToMp3(sourcePath, targetPath);
		voicePath = targetPath;
		
		//添加回答
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("content", content);
		inputMap.put("whoque", whoque);
		inputMap.put("type", type);
		inputMap.put("voice", voicePath);
		inputMap.put("voice_length", voice_length);
		
		Boolean isSuccess = false;
		List<Map<String, Object>> answerList = questionAnswerService.getAnswer(questionid, anwserwho);
		Map<String, Object> answer = null;
		if ( answerList.size()>0 ) {
			answer = answerList.get(0);
		}
		if ( answer!=null ) {//判断 问题 该律师是否已经回答过
			OutputUtil.returnJsonp(request, response, isSuccess.toString());
			return;
		}
		
		isSuccess = questionAnswerService.answerAddVoice(Integer.parseInt(questionid), anwserwho, inputMap);
		
		if ( isSuccess ) {
			Map<String, Object> inputMap1 = new HashMap<String, Object>();
			inputMap1.put("ISANSWER", 1);
			isSuccess = questionAnswerService.update(Integer.parseInt(questionid), inputMap);
		}
		
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	 /**
	 * 问题详细
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@RequestMapping("detail")
	public void detail (HttpServletRequest request, 
			HttpServletResponse response){
		String questionId = request.getParameter("questionId");
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		Map<String, Object> questionMap = questionAnswerService.questionMap(Integer.parseInt(questionId));
		if (questionMap.size() > 0) {
			Object phoneObj = questionMap.get("CREATEUSER");
			if (phoneObj != null) {
				String photoPath = registerService.getPhotoPath(phoneObj.toString());
				questionMap.put("imgPath", photoPath);
			}
		}
		Integer answerCount = questionAnswerService.answerCount(Integer.parseInt(questionId));
		questionMap.put("answerCount", answerCount);
		returnList.add(questionMap);
		List<Map<String, Object>> answerList = questionAnswerService.answerList(Integer.parseInt(questionId));
		Iterator<Map<String, Object>> iterator = answerList.iterator();
		while(iterator.hasNext()){
			Map<String, Object> next = iterator.next();
			Object phoneObj = next.get("ANWSERWHO");
			if (phoneObj != null) {
				next.put("imgPath", registerOfLawyerService.getPhotoPath(phoneObj.toString()));
			}
			
		}
		//将打开的回答设置为已读
		Boolean setRead = remindservice.setAnswerRead(Integer.parseInt(questionId));
		//TODO 设置状态错误时提示
		returnList.addAll(answerList);
		String json = JSONSerielizer.toJson(returnList, null, null);
		OutputUtil.returnJsonp(request, response, json);
	}
	 /**
	 * 对回答的回复
	 * @param request
	 * @param response
	 * @param user
	 * @param answerId
	 * @author ServerZhang
	 * @date 2016年4月5日
	 */
	@RequestMapping("evaluate/{user}/{answerId}")
	public void evaluate(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String user,
			@PathVariable Integer answerId){
		String content = request.getParameter("content");
		Boolean isSuccess = questionAnswerService.evaluateAnswer(answerId, user, content);
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param user
	 * @param answerId
	 * @author HelloWorld_Luo
	 * @date 2016年9月13日
	 */
	@RequestMapping("voiceEvaluate/{user}/{answerId}")
	public void voiceEvaluate(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String user,
			@PathVariable Integer answerId){
		String type = request.getParameter("type");
		String voice_length = request.getParameter("voice_length");
		//处理 录音
		String base64Voice = request.getParameter("voice_content");
		String voiceName = request.getParameter("voice_name");
		Boolean uploadVoiceInfor = false;
		String attachmentId = "";
		if (!StringUtils.isEmpty(voiceName) && !StringUtils.isEmpty(base64Voice)) {
			// 录音数据
			String[] split = base64Voice.split("base64,");
			String tempStr = split[1];
			String realStr = tempStr.substring(0, tempStr.length() - 2);
			byte[] decode = MyBase64.decode(realStr);

			// 上传录音及保存数据库
			Map<String, Object> others = new HashMap<String, Object>();
			others.put("saveType", PathManager.FILE_SAVE_TYPE_VOICE);// 必须的
			try {
				attachmentId = fileUploadService.upload1(
						ProjectInfor.PROJECT_NAME, user,
						PathManager.FILE_SAVE_TYPE_VOICE, voiceName, decode,
						others);
			} catch (ParameterNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String voicePath = "";
		if ( !StringUtils.isEmpty(attachmentId) ) {
			uploadVoiceInfor = true;
			voicePath = fileUploadService.getFilePathById(attachmentId);
		}
		
		String sourcePath = voicePath;
		String targetPath = voicePath.replaceFirst(".wav", ".mp3");
		fileUploadService.amrChangeToMp3(sourcePath, targetPath);
		voicePath = targetPath;
		
		if ( !uploadVoiceInfor ) {
			OutputUtil.returnJsonp(request, response, uploadVoiceInfor.toString());
			return;
		}
		
		//添加回答
		Map<String, Object> inputMap = new HashMap<String, Object>();
		inputMap.put("type", type);
		inputMap.put("voice", voicePath);
		inputMap.put("voicelength", voice_length);
		
		Boolean isSuccess = questionAnswerService.voiceEvaluateAnswer(answerId, user, inputMap);
		OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	/**
	 * 评价列表
	 * @param request
	 * @param response
	 * @param lawyerPhone
	 * @param answered
	 * @author ServerZhang
	 * @date 2016年4月1日
	 */
	@RequestMapping("eList/{answerId}")
	//@ResponseBody
	public void eList(HttpServletRequest request, 
			HttpServletResponse response,
			@PathVariable Integer answerId){
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> eList = questionAnswerService.evaluationList(answerId);
		
		Map<String, Object> quastionMap = new HashMap<String, Object>();
		if (eList.size() > 0) {
			Map<String, Object> answerMap = eList.get(0);
			Object qObj = answerMap.get("questionid");
			if (qObj != null) {
				quastionMap = questionAnswerService.questionMap(Integer.parseInt(qObj.toString()));
				if (quastionMap.size() > 0) {
					Object phoneObj = quastionMap.get("CREATEUSER");
					if (phoneObj != null) {
						String photoPath = registerService.getPhotoPath(phoneObj.toString());
						quastionMap.put("imgPath", photoPath);
					}
				}
			}
		}
		returnList.add(quastionMap);
		returnList.addAll(eList);
		OutputUtil.returnJsonp(request, response, JSONSerielizer.toJson(returnList, null, null));
	}
	@RequestMapping("lawyerEvalList")
	public void lawyerEvalList(HttpServletRequest request, 
			HttpServletResponse response){
		String lawyerPhone = request.getParameter("lawyerPhone");
		String questionId = request.getParameter("questionId");
		//System.out.println(questionId+"----"+lawyerPhone);
		List<Map<String, Object>> answerList = questionAnswerService.getAnswer(questionId, lawyerPhone);
		Map<String, Object> answer = new HashMap<String,Object>();
		if ( answerList.size()>0 ) {
			answer = answerList.get(0);
		}
		Integer answerId = Integer.parseInt(answer.get("ID").toString()) ;
		
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> eList = questionAnswerService.evaluationList(answerId);
		
		Map<String, Object> quastionMap = new HashMap<String, Object>();
		if (eList.size() > 0) {
			Map<String, Object> answerMap = eList.get(0);
			Object qObj = answerMap.get("questionid");
			if (qObj != null) {
				quastionMap = questionAnswerService.questionMap(Integer.parseInt(qObj.toString()));
				if (quastionMap.size() > 0) {
					Object phoneObj = quastionMap.get("CREATEUSER");
					if (phoneObj != null) {
						String photoPath = registerService.getPhotoPath(phoneObj.toString());
						quastionMap.put("imgPath", photoPath);
					}
				}
			}
			String pingjiawhoid = eList.get(0).get("ANWSERWHO").toString();
			String photoPath = fileUploadService.getFileByPhoneNoAndSaveType(pingjiawhoid, "LawyerPhoto");
			eList.get(0).put("imgPath", photoPath);
		}
		for (int i = 1; i < eList.size(); i++) {
			String pingjiawhoid = eList.get(i).get("PINGJIAWHOID").toString();
			String photoPath = "";
			if (lawyerPhone.equals(pingjiawhoid)) {
				photoPath = fileUploadService.getFileByPhoneNoAndSaveType(lawyerPhone, "LawyerPhoto");
			}else{
				photoPath = registerService.getPhotoPath(pingjiawhoid);
			}
			eList.get(i).put("imgPath", photoPath);
		}
		returnList.add(quastionMap);
		returnList.addAll(eList);
		OutputUtil.returnJsonp(request, response, JSONSerielizer.toJson(returnList, null, null));
	}
	 /**
	 * 定向解答问题详细
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年6月23日
	 */
	@RequestMapping("qDetail/{questionId}")
	public void qDetail (HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer questionId){
		List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
		Map<String, Object> questionMap = questionAnswerService.questionMap(questionId);
		if (questionMap.size() > 0) {
			Object phoneObj = questionMap.get("CREATEUSER");
			if (phoneObj != null) {
				String photoPath = registerService.getPhotoPath(phoneObj.toString());
				questionMap.put("imgPath", photoPath);
			}
		}
		List<Map<String, Object>> answerList = questionAnswerService.answerList(questionId);
		//将打开的回答设置为已读
		Boolean setRead = remindservice.setAnswerRead(questionId);
		//TODO 设置状态错误时提示
		
		returnList.add(questionMap);
		
		if (answerList.size() > 0) {
			Map<String, Object> answerMap = answerList.get(0);
			Object idObj = answerMap.get("Id");
			if (idObj != null) {
				List<Map<String, Object>> evaluationList = questionAnswerService.evaluationList(Integer.parseInt(idObj.toString()));
				returnList.addAll(evaluationList);
			}
		}else {
		}
		String json = JSONSerielizer.toJson(returnList, null, null);
		OutputUtil.returnJsonp(request, response, json);
	}
	 
	/**
	 * 有回答的问题列表
	 * 
	 * @param request
	 * @param response
	 * @param lawyerNo
	 * @author ServerZhang
	 * @date 2016年6月27日
	 */
	@RequestMapping("answeredList/{lawyerNo}")
	public void answeredList(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String lawyerNo) {

		DataVector<Map<String, Object>> listData = questionAnswerService
				.answeredList(lawyerNo, new RequestInput(request));
		List<Map<String, Object>> questionList = listData.getList();

		Iterator<Map<String, Object>> iterator = questionList.iterator();
		while (iterator.hasNext()) {
			Map<String, Object> next = iterator.next();
			Object idObj = next.get("id");
			if (idObj != null) {
				Integer answerCount = questionAnswerService.answerCount(Integer
						.parseInt(idObj.toString()));
				next.put("answerCount", answerCount);
			}
			Object userObj = next.get("createUser");
			if (userObj != null) {
				String photoPath = registerService.getPhotoPath(userObj
						.toString());
				next.put("imgPath", photoPath);
			}
		}
		String json = JSONSerielizer.toJson(questionList, null, null);
		response.addHeader("Access-Control-Allow-Origin", "*");
		OutputUtil.returnString(response, json);

	}
}
